package com.hljx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hljx.dto.CreateOrUpdateInfoDTO;
import com.hljx.dto.WorkloadTargetAdministrationCopyDTO;
import com.hljx.entity.*;
import com.hljx.filter.CommonMethod;
import com.hljx.mapper.sys.*;
import com.hljx.vo.WorkDeptInfoVO;
import com.hljx.vo.WorkProjectVO;
import com.hljx.vo.WorkloadTargetAdministrationDeptProjectVO;
import com.hljx.config.exception.DataException;
import com.hljx.dto.WorkloadTargetAdministrationDTO;
import com.hljx.dto.WorkloadTargetAdministrationInsertDTO;
import com.hljx.service.WorkloadTargetAdministrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 工作量指标管理主表 服务实现类
 * </p>
 *
 * @author hxc
 * @since 2023-09-18
 */
@Service
public class WorkloadTargetAdministrationServiceImpl extends ServiceImpl<WorkloadTargetAdministrationMapper, WorkloadTargetAdministration> implements WorkloadTargetAdministrationService {

    @Autowired
    private WorkloadTargetAdministrationMapper workloadTargetAdministrationMapper;

    @Autowired
    private WorkloadTargetDeptMapper workloadTargetDeptMapper;

    @Autowired
    private WorkloadTargetProjectMapper workloadTargetProjectMapper;

    @Autowired
    private WorkloadTargetBillDeptMapper workloadTargetBillDeptMapper;

    @Autowired
    private WorkloadTargetExeDeptMapper workloadTargetExeDeptMapper;

    @Autowired
    private CommonMethod commonMethod;


    /**
     * @param
     * @author hxc
     * @date 2023/9/18 17:22
     * @version 1.0
     * @see "添加工作量指标管理"
     */
    @Override
    @Transactional
    public int insertWorkloadTargetAdministration(WorkloadTargetAdministrationInsertDTO insertDTO) {
        //主表实体类

        try {
            //用户信息
            CreateOrUpdateInfoDTO createOrUpdateInfoDTO = new CreateOrUpdateInfoDTO(LocalDateTime.now(), commonMethod.getUser());

        //判重
        int distinct = workloadTargetAdministrationMapper.distinct(insertDTO);
        if (distinct>0){
            throw new DataException("工作量指标编码已存在",1002);
        }

        //添加主表信息
        int i = workloadTargetAdministrationMapper.insertWorkloadTarget(insertDTO.getPeriod(), insertDTO, LocalDateTime.now(),commonMethod.getUser());
        long  aLong;
        //获取新增的主键id
            aLong = workloadTargetAdministrationMapper.byCodeGetId(insertDTO.getPeriod(), insertDTO.getWorkloadCode());

        //核算单元科室实体类
        List<WorkloadTargetDept> dept = insertDTO.getDept();
        if (dept.size()>0) {
            //添加科室
            boolean i1 = workloadTargetDeptMapper.batchDeptInfo(dept, insertDTO.getPeriod(), aLong,createOrUpdateInfoDTO);
        }
        //开单科室
        List<WorkloadTargetBillDept> billDept = insertDTO.getBillDept();
        if (billDept.size()>0) {
            int i3 = workloadTargetBillDeptMapper.batchDeptInfo(billDept, insertDTO.getPeriod(), aLong,createOrUpdateInfoDTO);
        }
        //执行科室
        List<WorkloadTargetExeDept> exeDept = insertDTO.getExeDept();
        if (exeDept.size()>0) {
            int i4 = workloadTargetExeDeptMapper.batchDeptInfo(exeDept, insertDTO.getPeriod(), aLong,createOrUpdateInfoDTO);
        }
        //项目实体类
        int i2 = workloadTargetProjectMapper.batchProjectInfo(insertDTO.getProject(),insertDTO.getPeriod(),aLong,createOrUpdateInfoDTO);

        }catch (Exception e){
            throw  new DataException("新增失败",555);
        }
        return 1;
    }

    /**
     * @param
     * @author hxc
     * @date 2023/10/27 13:41
     * @version 1.0
     * @see       "编辑回显"
     */
    @Override
    public WorkloadTargetAdministrationDeptProjectVO updateWorkloadTargetAdministrationInfo(String period,Long id) {
        List<WorkloadTargetAdministrationDeptProjectVO> workloadTargetAdministrationDeptProjectVOS = workloadTargetAdministrationMapper.listUpdateWorkloadTargetAdministrationInfo(period, id);
        WorkloadTargetAdministrationDeptProjectVO workloadTargetAdministrationDeptProjectVO1 = new WorkloadTargetAdministrationDeptProjectVO();
        List<WorkProjectVO> workProjectVOS = new ArrayList<>();
        //获取项目信息
        for (WorkloadTargetAdministrationDeptProjectVO workloadTargetAdministrationDeptProjectVO:workloadTargetAdministrationDeptProjectVOS){
            WorkProjectVO workProjectVO = new WorkProjectVO();
            workProjectVO.setProjectId(workloadTargetAdministrationDeptProjectVO.getProjectId());
            workProjectVO.setProjectName(workloadTargetAdministrationDeptProjectVO.getProjectName());
            workProjectVO.setProjectCode(workloadTargetAdministrationDeptProjectVO.getProjectCode());
            workProjectVO.setUnit(workloadTargetAdministrationDeptProjectVO.getUnit());
            workProjectVO.setCount(workloadTargetAdministrationDeptProjectVO.getCount());

            workProjectVOS.add(workProjectVO);
        }
        workloadTargetAdministrationDeptProjectVO1.setProjectWork(workProjectVOS);
        //核算单元科室信息
        String deptCodeInfo = workloadTargetAdministrationDeptProjectVOS.get(0).getDeptCodeInfo();
      if ( StringUtils.isNotEmpty(deptCodeInfo)){
        String[] split = deptCodeInfo.split(",");
        List<String> strings = Arrays.asList(split);
        workloadTargetAdministrationDeptProjectVO1.setWorkDeptInfo(strings);
      }

        //拼接执行科室
        String billDeptCodeInfo = workloadTargetAdministrationDeptProjectVOS.get(0).getBillDeptCodeInfo();
        if ( StringUtils.isNotEmpty(billDeptCodeInfo)) {
            String[] split1 = billDeptCodeInfo.split(",");
            List<String> strings1 = Arrays.asList(split1);
            workloadTargetAdministrationDeptProjectVO1.setBillDeptWork(strings1);
        }
        //
        String exeDeptCodeInfo = workloadTargetAdministrationDeptProjectVOS.get(0).getExeDeptCodeInfo();
        if ( StringUtils.isNotEmpty(exeDeptCodeInfo)) {
            String[] split2 = exeDeptCodeInfo.split(",");
            List<String> strings2 = Arrays.asList(split2);
            workloadTargetAdministrationDeptProjectVO1.setExeDeptWork(strings2);
        }

        workloadTargetAdministrationDeptProjectVO1.setWorkloadName(workloadTargetAdministrationDeptProjectVOS.get(0).getWorkloadName());
        workloadTargetAdministrationDeptProjectVO1.setWorkloadCode(workloadTargetAdministrationDeptProjectVOS.get(0).getWorkloadCode());
        return workloadTargetAdministrationDeptProjectVO1;
    }

    /**
     * @param
     * @author hxc
     * @date 2023/9/20 15:00
     * @version 1.0
     * @see   "编辑"
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWorkloadTargetAdministration(WorkloadTargetAdministrationInsertDTO updateDTO) {

        //用户信息
        CreateOrUpdateInfoDTO createOrUpdateInfoDTO = new CreateOrUpdateInfoDTO(LocalDateTime.now(), commonMethod.getUser());


        if (updateDTO.getDept() != null && updateDTO.getDept().size()>0) {
            //删除科室信息
            int deleteDept = workloadTargetDeptMapper.ByWorkloadTargetAdministrationIdDeleteDept(updateDTO.getId(), updateDTO.getPeriod());
            //添加科室
            boolean i1 = workloadTargetDeptMapper.batchDeptInfo(updateDTO.getDept(), updateDTO.getPeriod(), updateDTO.getId(), createOrUpdateInfoDTO);
        }
        if (updateDTO.getBillDept() != null && updateDTO.getBillDept().size()>0) {
            //删除开单科室信息
            int deleteDept1 = workloadTargetBillDeptMapper.ByWorkloadTargetAdministrationIdDeleteDept(updateDTO.getId(), updateDTO.getPeriod());
            int i = workloadTargetBillDeptMapper.batchDeptInfo(updateDTO.getBillDept(), updateDTO.getPeriod(), updateDTO.getId(), createOrUpdateInfoDTO);
        }
        if (updateDTO.getExeDept() != null && updateDTO.getExeDept().size()>0) {
            //删除执行科室信息
            int deleteDept2 = workloadTargetExeDeptMapper.ByWorkloadTargetAdministrationIdDeleteDept(updateDTO.getId(), updateDTO.getPeriod());
            int i3 = workloadTargetExeDeptMapper.batchDeptInfo(updateDTO.getExeDept(), updateDTO.getPeriod(), updateDTO.getId(), createOrUpdateInfoDTO);
        }
        //删除项目信息
        int deleteProject = workloadTargetProjectMapper.ByWorkloadTargetAdministrationIdDeleteProject(updateDTO.getId(), updateDTO.getPeriod());
        //项目实体类
        int i2 = workloadTargetProjectMapper.batchProjectInfo(updateDTO.getProject(),updateDTO.getPeriod(),updateDTO.getId(),createOrUpdateInfoDTO);
        return i2;

    }

    /**
     * @param
     * @author hxc
     * @date 2023/9/19 11:22
     * @version 1.0
     * @see  "分页模糊查询"
     */
    @Override
    public IPage<WorkloadTargetAdministrationDeptProjectVO> listByPage(WorkloadTargetAdministrationDTO workloadTargetAdministrationDTO) {
        Page<WorkloadTargetAdministrationDeptProjectVO> page = new Page<>(workloadTargetAdministrationDTO.getPageNum(), workloadTargetAdministrationDTO.getPageSize());
        IPage<WorkloadTargetAdministrationDeptProjectVO> workloadTargetAdministrationDeptProjectVOIPage = workloadTargetAdministrationMapper.listByPage(page, workloadTargetAdministrationDTO);
        return workloadTargetAdministrationDeptProjectVOIPage;
    }

    /**
     * @param
     * @author hxc
     * @date 2023/9/20 14:30
     * @version 1.0
     * @see  "继承"
     */
    @Override
    @Transactional
    public int inheritWorkloadTargetAdministration(WorkloadTargetAdministrationCopyDTO copyDTO) {
        //继承之前把继承表中的数据全部删除
        int i3 = workloadTargetAdministrationMapper.truncateWorkloadTargetAdministration(copyDTO.getInheritPeriod());
        int i4 = workloadTargetDeptMapper.truncateWorkloadTargetDept(copyDTO.getInheritPeriod());
        int i5 = workloadTargetProjectMapper.truncateWorkloadTargetProject(copyDTO.getInheritPeriod());
        int i6 = workloadTargetBillDeptMapper.truncateWorkloadTargetDept(copyDTO.getInheritPeriod());
        int i7 = workloadTargetExeDeptMapper.truncateWorkloadTargetDept(copyDTO.getInheritPeriod());
        //继承

        int i = workloadTargetAdministrationMapper.inheritWorkloadTargetAdministration(copyDTO,commonMethod.getUser());
        int i1 = workloadTargetDeptMapper.inheritWorkloadTargetDept(copyDTO,commonMethod.getUser());
        int i2 = workloadTargetProjectMapper.inheritWorkloadTargetProject(copyDTO,commonMethod.getUser());
        int i8 = workloadTargetBillDeptMapper.inheritWorkloadTargetDept(copyDTO,commonMethod.getUser());
        int i9 = workloadTargetExeDeptMapper.inheritWorkloadTargetDept(copyDTO,commonMethod.getUser());

        return i2;
    }

    /**
     * @param
     * @author hxc
     * @date 2023/9/20 15:40
     * @version 1.0
     * @see "删除工作量指标管理"
     */
    @Override
    @Transactional
    public int deleteWorkloadAdministration(WorkloadTargetAdministrationDTO dto) {

        //用户信息
        CreateOrUpdateInfoDTO createOrUpdateInfoDTO = new CreateOrUpdateInfoDTO(LocalDateTime.now(), commonMethod.getUser());

        //删除工作量指标管理
        int i = workloadTargetAdministrationMapper.deleteByWorkloadTargetAdministrationId(dto.getPeriod(), dto.getId(),createOrUpdateInfoDTO);
        //删除科室信息
        int i1 = workloadTargetDeptMapper.deleteByWorkloadTargetDept(dto.getPeriod(), dto.getId(),createOrUpdateInfoDTO);
        int i3 = workloadTargetBillDeptMapper.deleteByBillDeptID(dto.getPeriod(), dto.getId());
        int deleteDept = workloadTargetExeDeptMapper.ByWorkloadTargetAdministrationIdDeleteDept(dto.getId(), dto.getPeriod());
        //删除项目信息
        int i2 = workloadTargetProjectMapper.deleteByWorkloadTargetProject(dto.getPeriod(), dto.getId(),createOrUpdateInfoDTO);

        return i2;
    }
}
